import type { UserTask } from '@/types/workflow';
import { useDocTypeCountStore } from '@/stores';
import type { FilterDocType } from '@/types/common';


export const setDocTypeCount =(list:UserTask[])=>{

    
    const docTypeCountStore = useDocTypeCountStore();
    docTypeCountStore.setCount(0,0,0,0,0,0,0);//重置

    let all = 0,receive=0,send=0,report=0,represent=0,ask=0,seal=0;

    list.forEach((task)=>{
        all++
        if(task.currentStatus?.workflowId == 1 || (task.docFormStatus >= 1  && task.docFormStatus <= 10)) send++;
        if(task.currentStatus?.workflowId == 2 || (task.docFormStatus >= 11 && task.docFormStatus <= 16)) receive++
        if(task.currentStatus?.workflowId == 3 || (task.docFormStatus >= 17 && task.docFormStatus <= 23)) report++
        if(task.currentStatus?.workflowId == 5 || (task.docFormStatus >= 24 && task.docFormStatus <= 29) || (task.docFormStatus >= 36 && task.docFormStatus <= 37)) ask++
        if(task.currentStatus?.workflowId == 6 || (task.docFormStatus >= 30 && task.docFormStatus <= 35) || task.docFormStatus == 38) seal++
        if(task.currentStatus?.workflowId == 7 || (task.docFormStatus >= 39 && task.docFormStatus <= 45)) represent++    
    })

    docTypeCountStore.setCount(all,send,receive,report,ask,seal,represent);
    console.log('all'+all)
}

export const filterTaskList =(list:UserTask[],filter:FilterDocType)=>{
    const result:UserTask[] = [];

    if(filter.all) return list;

    list.forEach((task)=>{
        if(filter.send){
            if(task.currentStatus?.workflowId == 1 || (task.docFormStatus >= 1  && task.docFormStatus <= 10))result.push(task);

        }else if(filter.receive){
            if(task.currentStatus?.workflowId == 2 || (task.docFormStatus >= 11 && task.docFormStatus <= 16)) result.push(task);

        }else if(filter.report){
            if(task.currentStatus?.workflowId == 3 || (task.docFormStatus >= 17 && task.docFormStatus <= 23)) result.push(task);

        }else if(filter.askLeave){
            if(task.currentStatus?.workflowId == 5 || (task.docFormStatus >= 24 && task.docFormStatus <= 29) || (task.docFormStatus >= 36 && task.docFormStatus <= 37)){
                result.push(task);
            }

        }else if(filter.seal){
            if(task.currentStatus?.workflowId == 6 || (task.docFormStatus >= 30 && task.docFormStatus <= 35) || task.docFormStatus == 38) {
                result.push(task)
            }

        }else if(filter.represent){
            if(task.currentStatus?.workflowId == 7 || (task.docFormStatus >= 39 && task.docFormStatus <= 45)){
                result.push(task)
            }
        }


    })

    return result;
}